﻿using System.Data;
using System.Data.SqlClient;
using System.IO;

namespace UDS.Components
{
    public class BBSReplay
    {
        public int ReplayId
        {
            get;
            set;
        }

        public int ItemID
        {
            get;
            set;
        }

        public string Content
        {
            get;
            set;
        }

        public string Replayer
        {
            get;
            set;
        }

        public string ReplayTime
        {
            get;
            set;
        }

        public string ReplayIP
        {
            get;
            set;
        }

        /// <summary>
        /// 将附件和回复绑定
        /// </summary>
        /// <param name="filenames">文件名,多个文件名用 , 分开</param>
        public void Attach(string filenames)
        {
            var data = new Database();
            string[] arrfilename = filenames.Split(',');
            for (int i = 0; i < arrfilename.Length; i++)
            {
                if (arrfilename[i].Trim() != "")
                {
                    var parameters = new[]
                    {
                        Database.MakeInParam("@replayid", SqlDbType.Int, 4, ReplayId),
                        Database.MakeInParam("@filename", SqlDbType.NVarChar, 500, arrfilename[i])
                    };
                    data.RunProc("dbo.sproc_BBS_AttachmentToReplay", parameters);
                }
            }
        }

        /// <summary>
        /// 删除附件
        /// </summary>
        public void DelAttachment(string attachmentmd)
        {
            var data = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@replayid", SqlDbType.Int, 4, ReplayId),
            };
            using (SqlDataReader dr = data.RunDataReader("dbo.sproc_BBS_GetReplayAttachment", parameters))
            {
                while (dr.Read())
                {
                    File.Delete(attachmentmd + "\\" + dr["filename"]);
                }
            }
            SqlParameter[] prams1 = {
                Database.MakeInParam("@replayid", SqlDbType.Int, 4, ReplayId),
            };
            data.RunProc("dbo.sproc_BBS_DelAttachmentToReplay", prams1);
        }
    }
}